package com.pureut.system.service.impl;

import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.system.domain.SysShopModeling;
import com.pureut.system.domain.dto.ModelingMqDto;
import com.pureut.system.domain.dto.ProjectModelingDto;
import com.pureut.system.domain.dto.SysLineDto;
import com.pureut.system.domain.vo.SysShopModelingVo;
import com.pureut.system.mapper.SysShopModelingMapper;
import com.pureut.system.service.ISysShopModelingService;
import com.pureut.system.util.MaterialMqUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description 车间建模实现类
 * @Date 22-12-23
 * @Author hl
 **/
@Service
public class SysShopModelingServiceImpl implements ISysShopModelingService {

    @Resource
    private SysShopModelingMapper sysShopModelingMapper;


    /**
     * 车间建模实现类
     *
     * @param sysShopModeling 车间建模接受
     * @return List<MaterialFinishedDto> 车间建模返回
     */
    @Override
    public List<SysShopModeling> selectShopModelingList(SysShopModelingVo sysShopModeling) {
        List<SysShopModeling> list = sysShopModelingMapper.selectShopModelingList(sysShopModeling);
        SysShopModelingVo sysShopModeling2 = new SysShopModelingVo();
        List<SysShopModeling> list2 = sysShopModelingMapper.selectShopModelingList(sysShopModeling2);
        Map<Long, String> actualMap = list2.stream().collect(Collectors.toMap(SysShopModeling::getModelingId, SysShopModeling::getModelingCode));
        if (list.size() > 0) {
            for (SysShopModeling sys :
                    list) {
                int type = sys.getModelingType();
                if (type == 0) {
                    sys.setModelingTypeDict("工厂");
                } else if (type == 1) {
                    sys.setModelingTypeDict("车间");
                } else {
                    sys.setModelingTypeDict("线别");
                }
                if (sys.getParentId() != 0) {
                    sys.setSuperiorCode(actualMap.get(sys.getParentId()));
                }
            }
        }
        actualMap.clear();
        return list;
    }

    /**
     * 车间建模新增
     *
     * @param sysShopModeling 车间建模接受
     * @return 成功 1 失败 0
     */
    @Override
    public int insertShopModeling(SysShopModeling sysShopModeling) throws Exception {
        int type = sysShopModeling.getModelingType();
        if (type == 2) {
            //代表线别
            if (sysShopModeling.getModelingShopRank() == 0) {
                throw new GlobalException("新增线别的时候阶别不允许为空!");
            }
        }
        int in = sysShopModelingMapper.checkUniqueness(sysShopModeling.getModelingCode());
        if (in > 0) {
            throw new SQLException("区域编码不能重复");
        }
        if (type == 0) {
            sysShopModeling.setParentId(0);//代表工厂
        }
        //mq发送消息
        ModelingMqDto modelingMqDto = new ModelingMqDto();
        BeanUtils.copyProperties(sysShopModeling, modelingMqDto);
        MaterialMqUtil.modelingMqSend(modelingMqDto);
        return sysShopModelingMapper.insertShopModeling(sysShopModeling);
    }

    /**
     * 车间建模树结构展示
     *
     * @return List<ProjectModelingDto> 树结构
     */
    @Override
    public List<ProjectModelingDto> selectChildrenModelingThe() {
        List<ProjectModelingDto> list = sysShopModelingMapper.selectChildrenModelingThe();
        for (ProjectModelingDto pro :
                list) {
            String type = pro.getTypeName();
            if (type.equals("0")) {
                pro.setTypeName("工厂");
            } else if (type.equals("1")) {
                pro.setTypeName("车间");
            } else {
                pro.setTypeName("线别");
            }
        }
        return buildMenuTree(list);
    }


    /**
     * 查询单个车间建模
     *
     * @param modelingId 车间建模id
     * @return SysShopModeling 返回类
     */
    @Override
    public SysShopModeling selectModelingById(long modelingId) {
        SysShopModelingVo sysShopModeling = new SysShopModelingVo();
        sysShopModeling.setModelingId(modelingId);
        List<SysShopModeling> list = sysShopModelingMapper.selectShopModelingList(sysShopModeling);
        SysShopModeling sys = list.get(0);
        if (sys.getParentId() != 0) {
            sys.setSuperiorCode(sysShopModelingMapper.selectStrShopModeling(sys.getParentId()));
        }
        int type = sys.getModelingType();
        if (type == 0) {
            sys.setModelingTypeDict("工厂");
        } else if (type == 1) {
            sys.setModelingTypeDict("车间");
        } else {
            sys.setModelingTypeDict("线别");
        }
        return sys;
    }

    /**
     * 修改车间建模
     *
     * @param sysShopModeling 车间建模实体
     * @return 成功 1 失败 0
     */
    @Override
    public int updateShopModelingEdit(SysShopModeling sysShopModeling) throws Exception {
        //mq发送消息
        ModelingMqDto modelingMqDto = new ModelingMqDto();
        BeanUtils.copyProperties(sysShopModeling, modelingMqDto);
        MaterialMqUtil.modelingMqSend(modelingMqDto);
        return sysShopModelingMapper.updateShopModelingEdit(sysShopModeling);
    }

    /**
     * 删除车间建模
     *
     * @param modelingId 车间建模Id
     * @return 存在子节点 2 成功 1 失败 0
     */
    @Override
    public AjaxResult deleteModelingById(String modelingId) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        String[] array = modelingId.split(",");
        List<String> modelingIds = Arrays.asList(modelingId.split(","));
        String sysShopModelings = sysShopModelingMapper.selectModelingByIds(modelingIds);
        ModelingMqDto mq = new ModelingMqDto();
        mq.setModelingCode(sysShopModelings);
        mq.setMark(1);
        MaterialMqUtil.modelingMqSendDelete(mq);
        int cv = sysShopModelingMapper.judgmentModelingId(array);
        if(cv>0){
            return ajax.error("删除失败,存在子节点无法删除");
        }
        int xian = sysShopModelingMapper.lineModelingId(array);
        if(xian>0){
            return ajax.error("有关联线边仓，不能进行删除！");
        }
        int os = sysShopModelingMapper.deleteModelingById(array);
        if (os > 0) {
            return ajax.success("删除成功");
        }
        return ajax;
    }

    /**
     * 车间建模车间查询
     *
     * @return
     */
    @Override
    public List<SysShopModeling> getWorkshop() {
        return sysShopModelingMapper.getWorkshopName();
    }

    /**
     * 车间建模线别查询
     *
     * @return
     */
    @Override
    public List<SysShopModeling> getLine() {
        return sysShopModelingMapper.getLineName();
    }


    /**
     * 根据车间id获取线别
     */
    @Override
    public List<SysLineDto> getModelingLine(long modelingId) {
        return sysShopModelingMapper.getModelingLine(modelingId);
    }

    /**
     * 根据线别编码获取车间id
     *
     * @param lineCode
     * @return
     */
    @Override
    public SysShopModeling getWorkshopId(String lineCode) {

        return sysShopModelingMapper.getWorkshopId(lineCode);
    }

    /**
     * 获取脱脂线别
     *
     * @return
     */
    @Override
    public List<SysLineDto> getDegreasLine() {
        return sysShopModelingMapper.getDegreasLine();
    }

    /**
     * 获取分切线别
     *
     * @return
     */
    @Override
    public List<SysLineDto> getCutLine() {
        List<SysLineDto> cutLine = sysShopModelingMapper.getCutLine();
        return cutLine;
    }

    /**
     * 获取水刺线别
     *
     * @return
     */
    @Override
    public List<SysLineDto> getSphinxLine() {
        return sysShopModelingMapper.getSphinxLine();
    }

    /**
     * 获取分切下的车间
     *
     * @return
     */
    @Override
    public List<SysShopModeling> getCutShopModeling() {
        return sysShopModelingMapper.getCutShopModeling();
    }

    /**
     * 获取脱脂下的车间
     *
     * @return
     */
    @Override
    public List<SysShopModeling> getDegreasShopModeling() {
        return sysShopModelingMapper.getDegreasShopModeling();
    }

    /**
     * 根据线别编码获取id
     *
     * @param lineCode
     * @return
     */
    @Override
    public Long getLineId(String lineCode) {

        return sysShopModelingMapper.getLineId(lineCode);
    }

    /**
     * 根据线别编码获取车间编码(fegin调用)
     *
     * @param lineCode
     * @return
     */
    @Override
    public String getWorkshopCode(String lineCode) {

        return sysShopModelingMapper.getWorkshopCode(lineCode);
    }

    @Override
    public Long getStorage(String lineCode) {
        return sysShopModelingMapper.getStorage(lineCode);
    }

    /**
     * 根据车间编码获取线别名称
     *
     * @param lineCode
     * @return
     */
    @Override
    public SysLineDto getModelingLineInfo(String lineCode) {
        return sysShopModelingMapper.getModelingLineInfo(lineCode);
    }


    /**
     * 树结构递归
     *
     * @param
     * @return
     */
    public static List<ProjectModelingDto> buildMenuTree(List<ProjectModelingDto> menus) {
        List<ProjectModelingDto> returnList = new ArrayList<ProjectModelingDto>();
        List<Long> tempList = new ArrayList<Long>();
        for (ProjectModelingDto dept : menus) {
            tempList.add(dept.getId());
        }
        for (Iterator<ProjectModelingDto> iterator = menus.iterator(); iterator.hasNext(); ) {
            ProjectModelingDto menu = (ProjectModelingDto) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFn(menus, menu);
                if (menu.getParentId() == 0) {
                    returnList.add(menu);
                }
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private static void recursionFn(List<ProjectModelingDto> list, ProjectModelingDto t) {
        // 得到子节点列表
        List<ProjectModelingDto> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ProjectModelingDto tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 递归列表
     */
    private static List<ProjectModelingDto> getChildList(List<ProjectModelingDto> list, ProjectModelingDto t) {
        List<ProjectModelingDto> tlist = new ArrayList<ProjectModelingDto>();
        Iterator<ProjectModelingDto> it = list.iterator();
        while (it.hasNext()) {
            ProjectModelingDto n = (ProjectModelingDto) it.next();
            if (n.getParentId().longValue() == t.getId()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private static boolean hasChild(List<ProjectModelingDto> list, ProjectModelingDto t) {
        return getChildList(list, t).size() > 0;
    }

}
